import 'package:after_sale_plugin/package_bridge.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'package:rxdart/subjects.dart';

// 事件枚举
enum AfterSalePluginActionEnum {
  showToast,
  showLoading,
  dismissLoading,
  showConfirmAlert,
  getCountryCode,
  connectVehicle,
  selectVehicle,
  selectAsssets,
  uploadAsssets,
  checkError,
  getProductManual,
  getVideoGuide,
  playVideo,
  playPicture,
  newOrder,
  updateOrder,
  orderList,
  orderDetail,
  orderResolved,
  orderUnsolved,
  getRegion,
}

class PackageEvent {
  // 响应
  static void _action(
    AfterSalePluginActionEnum type,
    EventCallBack? callBack, {
    dynamic param,
  }) {
    if (AfterSalePluginBridge.to.isRunAlone) {
      if (callBack != null) {
        callBack(true);
      }
    } else {
      AfterSalePluginBridge.to.callBackToApp!(type, param, (rsp) {
        if (callBack != null) {
          callBack(rsp);
        }
      });
    }
  }

  // 显示 Toast
  static void showToast(String content) {
    _action(AfterSalePluginActionEnum.showToast, null, param: content);
  }

  // 显示 加载框
  static void showLoading() {
    _action(AfterSalePluginActionEnum.showLoading, null, param: null);
  }

  // 隐藏 加载框
  static void dismissLoading() {
    _action(AfterSalePluginActionEnum.dismissLoading, null, param: null);
  }

  // 显示 确认弹框
  static void showConfirmAlert(
    String content, {
    String? confirmTitle,
    String? cancelTitle,
    bool isShowCancel = true,
    EventCallBack? confirmCallBack,
  }) {
    _action(
      AfterSalePluginActionEnum.showConfirmAlert,
      confirmCallBack,
      param: {
        'content': content,
        'confirm_title': AppLanguageUtil.to.titleConfirm,
        'cancel_title': cancelTitle ?? AppLanguageUtil.to.titleCancel,
        'show_cancel': isShowCancel,
      },
    );
  }

  // 连接车辆
  static void connectVehicle(EventCallBack? callBack, {param}) {
    _action(AfterSalePluginActionEnum.connectVehicle, callBack, param: param);
  }

  // 获取国家/地区
  static void getCountryCode(EventCallBack? callBack, {param}) {
    _action(AfterSalePluginActionEnum.getCountryCode, callBack, param: param);
  }

  // 获取认证的车辆列表
  static void selectVehicle(EventCallBack? callBack, {param}) {
    _action(AfterSalePluginActionEnum.selectVehicle, callBack, param: param);
  }

  // 选择图片/视频
  static void selectAsssets(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.selectAsssets, callBack, param: param);
  }

  // 上传图片/视频
  static void uploadAsssets(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.uploadAsssets, callBack, param: param);
  }

  // 播放视频
  static void playVideo(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.playVideo, callBack, param: param);
  }

  // 播放图片
  static void playPicture(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.playPicture, callBack, param: param);
  }

  // 检测错误
  static void startCheckErr(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.checkError, callBack, param: param);
  }

  // 获取产品手册
  static void getProductManual(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.getProductManual, callBack, param: param);
  }

  // 获取视频指南
  static void getVideoGuide(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.getVideoGuide, callBack, param: param);
  }

  // 提交工单
  static void newOrder(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.newOrder, callBack, param: param);
  }

  // 修改工单
  static void updateOrder(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.updateOrder, callBack, param: param);
  }

  // 工单详情
  static void orderDetail(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.orderDetail, callBack, param: param);
  }

  // 工单列表
  static void orderList(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.orderList, callBack, param: param);
  }

  // 工单已解决
  static void orderResolved(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.orderResolved, callBack, param: param);
  }

  // 工单未解决
  static void orderUnsolved(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.orderUnsolved, callBack, param: param);
  }

  // 获取区域
  static void getRegion(
    EventCallBack? callBack, {
    param,
  }) {
    _action(AfterSalePluginActionEnum.getRegion, callBack, param: param);
  }
}

// 事件通知
class PackageEventStream {
  // 单例
  static PackageEventStream? _instance;

  PackageEventStream._internal() {
    _instance = this;
  }

  factory PackageEventStream() => _instance ?? PackageEventStream._internal();

  final _subject = BehaviorSubject<String>();

  // 监听事件
  Stream<String> get on => _subject.stream; // 返回一个Stream供外部监听事件

  // 发送事件
  void fire(String event) {
    _subject.sink.add(event);
  }

  // 关闭流，清理资源
  dispose() {
    _subject.close();
  }
}
